Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents lblTitle As System.Windows.Forms.Label Friend WithEvents btnFuel As System.Windows.Forms.Button Friend WithEvents BtnQuit As System.Windows.Forms.Button Friend WithEvents TxtLocation As System.Windows.Forms.TextBox Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents TxtPoints As System.Windows.Forms.TextBox Friend WithEvents BtnGo As System.Windows.Forms.Button Friend WithEvents Label8 As System.Windows.Forms.Label Friend WithEvents TxtGas As System.Windows.Forms.TextBox Friend WithEvents Label9 As System.Windows.Forms.Label Friend WithEvents TxtCash As System.Windows.Forms.TextBox Friend WithEvents Label11 As System.Windows.Forms.Label Friend WithEvents LstPts As System.Windows.Forms.ListBox Private Sub InitializeComponent() Me.lblTitle = New System.Windows.Forms.Label Me.btnFuel = New System.Windows.Forms.Button Me.BtnQuit = New System.Windows.Forms.Button Me.TxtLocation = New System.Windows.Forms.TextBox Me.Label1 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.TxtPoints = New System.Windows.Forms.TextBox Me.BtnGo = New System.Windows.Forms.Button Me.Label8 = New System.Windows.Forms.Label Me.TxtGas = New System.Windows.Forms.TextBox Me.Label9 = New System.Windows.Forms.Label Me.TxtCash = New System.Windows.Forms.TextBox Me.LstPts = New System.Windows.Forms.ListBox Me.Label11 = New System.Windows.Forms.Label Me.SuspendLayout() ' 'lblTitle ' Me.lblTitle.AutoSize = True Me.lblTitle.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.2!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.lblTitle.Location = New System.Drawing.Point(48, 24) Me.lblTitle.Name = "lblTitle" Me.lblTitle.Size = New System.Drawing.Size(115, 23) Me.lblTitle.TabIndex = 0 Me.lblTitle.Text = "CSC 230 Toy" ' 'btnFuel ' Me.btnFuel.Location = New System.Drawing.Point(248, 256) Me.btnFuel.Name = "btnFuel" Me.btnFuel.TabIndex = 1 Me.btnFuel.Text = "Get Fuel" ' 'BtnQuit ' Me.BtnQuit.Location = New System.Drawing.Point(424, 256) Me.BtnQuit.Name = "BtnQuit" Me.BtnQuit.TabIndex = 2 Me.BtnQuit.Text = "Quit" ' 'TxtLocation ' Me.TxtLocation.Location = New System.Drawing.Point(152, 104) Me.TxtLocation.Name = "TxtLocation" Me.TxtLocation.ReadOnly = True Me.TxtLocation.Size = New System.Drawing.Size(96, 22) Me.TxtLocation.TabIndex = 3 Me.TxtLocation.Text = "Philly" ' 'Label1 ' Me.Label1.Location = New System.Drawing.Point(32, 104) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(112, 24) Me.Label1.TabIndex = 4 Me.Label1.Text = "Current Location" ' 'Label2 ' Me.Label2.Location = New System.Drawing.Point(32, 72) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(112, 24) Me.Label2.TabIndex = 6 Me.Label2.Text = "Current Points" ' 'TxtPoints ' Me.TxtPoints.Location = New System.Drawing.Point(152, 72) Me.TxtPoints.Name = "TxtPoints" Me.TxtPoints.ReadOnly = True Me.TxtPoints.Size = New System.Drawing.Size(96, 22) Me.TxtPoints.TabIndex = 5 Me.TxtPoints.Text = "0" ' 'BtnGo ' Me.BtnGo.Location = New System.Drawing.Point(336, 256) Me.BtnGo.Name = "BtnGo" Me.BtnGo.TabIndex = 17 Me.BtnGo.Text = "Go" ' 'Label8 ' Me.Label8.Location = New System.Drawing.Point(32, 136) Me.Label8.Name = "Label8" Me.Label8.Size = New System.Drawing.Size(112, 24) Me.Label8.TabIndex = 19 Me.Label8.Text = "Current Gas:" ' 'TxtGas ' Me.TxtGas.Location = New System.Drawing.Point(152, 136) Me.TxtGas.Name = "TxtGas" Me.TxtGas.ReadOnly = True Me.TxtGas.Size = New System.Drawing.Size(96, 22) Me.TxtGas.TabIndex = 18 Me.TxtGas.Text = "10" ' 'Label9 ' Me.Label9.Location = New System.Drawing.Point(32, 168) Me.Label9.Name = "Label9" Me.Label9.Size = New System.Drawing.Size(112, 24) Me.Label9.TabIndex = 21 Me.Label9.Text = "Current Cash:" ' 'TxtCash ' Me.TxtCash.Location = New System.Drawing.Point(152, 168) Me.TxtCash.Name = "TxtCash" Me.TxtCash.ReadOnly = True Me.TxtCash.Size = New System.Drawing.Size(96, 22) Me.TxtCash.TabIndex = 20 Me.TxtCash.Text = "100" ' 'LstPts ' Me.LstPts.ItemHeight = 16 Me.LstPts.Location = New System.Drawing.Point(296, 56) Me.LstPts.Name = "LstPts" Me.LstPts.Size = New System.Drawing.Size(168, 180) Me.LstPts.TabIndex = 24 ' 'Label11 ' Me.Label11.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.2!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label11.Location = New System.Drawing.Point(288, 16) Me.Label11.Name = "Label11" Me.Label11.Size = New System.Drawing.Size(280, 32) Me.Label11.TabIndex = 25 Me.Label11.Text = "Points for Traveling to:" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15) Me.BackColor = System.Drawing.Color.Red Me.ClientSize = New System.Drawing.Size(696, 384) Me.Controls.Add(Me.Label11) Me.Controls.Add(Me.LstPts) Me.Controls.Add(Me.Label9) Me.Controls.Add(Me.TxtCash) Me.Controls.Add(Me.Label8) Me.Controls.Add(Me.TxtGas) Me.Controls.Add(Me.BtnGo) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.TxtPoints) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.TxtLocation) Me.Controls.Add(Me.BtnQuit) Me.Controls.Add(Me.btnFuel) Me.Controls.Add(Me.lblTitle) Me.Name = "Form1" Me.Text = "Silly Game" Me.ResumeLayout(False) End Sub #End Region Const UPPER As Integer = 9 Const LOWER As Integer = 1 Const gasPrice As Double = 2.0 'Const gasToTravel As Double = 1.0 Const MPG As Double = 20.0 Dim inpf As IO.StreamReader Dim distf As IO.StreamReader Dim distances(30, 30) As Integer Dim cities(30) As String Dim numLoc As Integer = 0 Dim currLocIndex As Integer Private Function randPts() As Integer Dim rand As Integer rand = Convert.ToInt32((UPPER - LOWER + 1) * Rnd() + LOWER) Return rand End Function Private Function calcGasToTravel(ByVal dist(,) As Integer, ByVal startInd As Integer, ByVal endInd As Integer) As Double Dim gas As Double Dim distance As Integer distance = dist(startInd, endInd) gas = distance / MPG Return gas End Function Private Sub BtnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnQuit.Click Me.Close() End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim rand As Integer Dim row As Integer Dim col As Integer ' seed random number generator based on time Randomize() ' read cities in along with distances between them 'If IO.File.Exists("cities.txt") Then 'inpf = IO.File.OpenText("cities.txt") If IO.File.Exists("morecities.txt") Then inpf = IO.File.OpenText("morecities.txt") Do While inpf.Peek <> -1 And numLoc <= 30 Dim currLine As String currLine = inpf.ReadLine() cities(numLoc) = currLine numLoc = numLoc + 1 Loop inpf.Close() ' now read distances from other file 'If IO.File.Exists("distances.txt") Then 'distf = IO.File.OpenText("distances.txt") If IO.File.Exists("moredistances.txt") Then distf = IO.File.OpenText("moredistances.txt") Do While distf.Peek <> -1 And row < numLoc Dim rowStr As String rowStr = distf.ReadLine() col = 0 Dim remStr As String remStr = rowStr Do While col < numLoc - 1 Dim commapos As Integer commapos = remStr.IndexOf(",") Dim currStr As String currStr = remStr.Substring(0, commapos) remStr = remStr.Substring(commapos + 1) Dim val As Integer val = Convert.ToInt32(currStr) distances(row, col) = val col = col + 1 Loop ' get last distance in row distances(row, col) = Convert.ToInt32(remStr) row = row + 1 Loop Else MsgBox("distance file not found, program cannot run") End If Else MsgBox("city file not found, program cannot run") End If ' put us in the first city TxtLocation.Text = cities(0) currLocIndex = 0 ' fill other point values Dim cnt As Integer LstPts.Items.Add(cities(0) & ", 0") For cnt = 1 To numLoc - 1 Dim line As String rand = randPts() line = cities(cnt) & ", " & rand LstPts.Items.Add(line) Next End Sub Private Sub BtnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnGo.Click Dim newPts As Integer Dim pts As Integer Dim gasAvail As Double Dim gasToTravel As Double Dim endLoc As Integer Dim rand As Integer endLoc = LstPts.SelectedIndex gasAvail = Convert.ToDouble(TxtGas.Text) If endLoc <> -1 Then gasToTravel = calcGasToTravel(distances, currLocIndex, endLoc) If gasAvail >= gasToTravel Then pts = Convert.ToInt32(TxtPoints.Text) ' determine points for going there Dim selectLine As String selectLine = LstPts.SelectedItem Dim commapos As Integer commapos = selectLine.IndexOf(",") Dim ptsStr As String ptsStr = selectLine.Substring(commapos + 1) newPts = Convert.ToInt32(ptsStr) ' move us to the new location TxtLocation.Text = cities(endLoc) currLocIndex = endLoc ' generate new points values LstPts.Items.Clear() Dim cnt As Integer For cnt = 0 To numLoc - 1 If cnt = endLoc Then ' going to same city isn't worth any points LstPts.Items.Add(cities(cnt) & ", 0") Else ' generate random points for going there Dim line As String rand = randPts() line = cities(cnt) & ", " & rand LstPts.Items.Add(line) End If Next pts = pts + newPts TxtPoints.Text = Convert.ToString(pts) gasAvail = gasAvail - gasToTravel TxtGas.Text = gasAvail.ToString("n1") Else MsgBox("not enough gas to travel") End If End If End Sub Private Sub btnFuel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFuel.Click Dim moneyAvail As Double Dim gasAvail As Double moneyAvail = Convert.ToDouble(TxtCash.Text) gasAvail = Convert.ToDouble(TxtGas.Text) If moneyAvail >= gasPrice Then moneyAvail = moneyAvail - gasPrice gasAvail = gasAvail + 1 TxtCash.Text = moneyAvail.ToString("n2") TxtGas.Text = gasAvail.ToString("n1") Else MsgBox("not enough money to buy gas") End If End Sub End Class